from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from course import models,courseser

# Create your views here.
#获取到首页的一级分类以及孩子
class Category(APIView):
    def get(self,request):
        allcate=models.CategoryModel.objects.filter(parent=None,ishome=True,isshow=True).order_by('order')
        ser=courseser.CategorySerializer(allcate,many=True)
        for i in ser.data:
            i['children']=i['children'][:3]
        return Response({"msg":"ok","data":ser.data,"code":200})

#获取到当前的分类
class NowCate(APIView):
    def get(self,request):
        id=request.query_params.get('id')
        info=models.CategoryModel.objects.filter(id=id,parent=None,ishome=True,isshow=True).first()
        ser=courseser.CategorySerializer(info)
        return Response({"msg":"ok","data":ser.data,"code":200})
